package com.cms.common.kit;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;

import com.cms.common.util.pass.JsonUtil;

/**
 * 用于处理返回json的工具类
 * @author life
 *
 */
public class ResponseKit {

	/**
	 * 返回处理成功的json
	 * @param message
	 * @return
	 */
	public static String responseSuccess(String message){
		ResMessage rm=new ResMessage(message);
		return JsonUtil.toJson(rm);
	}
	
	/**
	 * 返回处理失败的json
	 * @param message
	 * @return
	 */
	public static String responseError(String message){
		ResMessage rm=new ResMessage(ResMessage.ERROR_CODE,message);
		return JsonUtil.toJson(rm);
	}
	
	
	/**
	 * 写入cookies
	 * @param name 名称
	 * @param value 值
	 * @param cookieTime 有效期
	 * @param domain 域名
	 * @param path 
	 */
	public static void setCookie(HttpServletResponse response, String name,String value,int cookieTime,String domain,String path){
		  Cookie _cookie = new Cookie(name ,value);
		  _cookie.setMaxAge(cookieTime);
		  _cookie.setDomain(domain);
		  _cookie.setPath(path);
		  response.addCookie(_cookie);
	}
	
	/**
	 * 默认当前域名，有效期为7天，路径为当前路径下
	 * @param response
	 * @param name
	 * @param value
	 */
	public static void setCookie(HttpServletResponse response, String name,String value){
		  Cookie _cookie = new Cookie(name ,value);
		  _cookie.setMaxAge(1000*60*60*24*7);
		  response.addCookie(_cookie);
	}

	/**
	 * 清除系统的cookie
	 * @param res
	 */
	public static void clearCookies(HttpServletResponse res) {
		Cookie loginCookie=new Cookie(Constant.CURRENT_ADMIN_COOKIE_INFO,"");
		loginCookie.setMaxAge(0);
		res.addCookie(loginCookie);
	}
	
	
	
}
